//
//  ViewController.m
//  CocoapodsOC
//
//  Created by 刘培壮 on 2018/10/12.
//  Copyright © 2018 com.blue. All rights reserved.
//

#import "ViewController.h"
#import "SecondViewController.h"
#import <Typhoon.h>
#import <AFNetworking.h>
#import "ViewModel.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *userNameInput;
@property (weak, nonatomic) IBOutlet UITextField *passwordInput;

@property (weak, nonatomic) IBOutlet UIButton *button;

@property (nonatomic, strong) ViewModel *vm;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.vm = ViewModel.new;

    [self loginLogic];
}

- (void)nextPage {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self performSegueWithIdentifier: @"second" sender: nil];
    });
}

- (void)loginLogic {
    RAC(self.vm, username) = self.userNameInput.rac_textSignal;
    
    RAC(self.vm, password) = self.passwordInput.rac_textSignal;
    
    RACSignal *usernameSignal = [self.userNameInput.rac_textSignal map:^id(NSString *string) {
        if (string.length >= 5) {
            return [UIColor greenColor];
        } else {
            return [UIColor redColor];
        }
    }];
    
    RACSignal *passwordSignal = [self.passwordInput.rac_textSignal map:^id(NSString *string) {
        if (string.length >= 5) {
            return [UIColor greenColor];
        } else {
            return [UIColor redColor];
        }
    }];
    
    RAC(self.userNameInput, textColor) = usernameSignal;
    RAC(self.passwordInput, textColor) = passwordSignal;
    
    RACSignal *combineSignal = [RACSignal combineLatest:@[usernameSignal, passwordSignal]];
    
    RACSignal *signal = [combineSignal map:^id (RACTuple *tuple) {
        UIColor *userNameColor = tuple.first;
        UIColor *passwordColor = tuple.last;
        return @(userNameColor == [UIColor greenColor] && passwordColor == [UIColor greenColor]);
    }];
    
    self.button.rac_command = [[RACCommand alloc]initWithEnabled:signal signalBlock:^RACSignal *(id  _Nullable input) {
        [self.vm login];
        NSLog(@"Button was pressed");
        return [RACSignal empty];
    }];
}

- (void)typhoonExerciese {
    [self.animal eat];
}

- (void)racExercise {
    NSArray *array = @[@1, @2, @3];
    RACSequence *stream = [array rac_sequence];
    
    NSArray *array1 = [[stream map:^id _Nullable(id  _Nullable value) {
        return @([value integerValue] + 2);
    }] array];
    
    NSLog(@"array1 = %@", array1);
    
    NSArray *array2 = [[stream filter:^BOOL (id value) {
        return [value integerValue] % 2;
    }] array];
    NSLog(@"array2 = %@", array2);
    
    NSString *string1 = [[stream map:^id _Nullable(id  _Nullable value) {
        return [value stringValue];
    }] foldLeftWithStart:@"" reduce:^id _Nullable(id  _Nullable accumulator, id  _Nullable value) {
        return [accumulator stringByAppendingString:value];
    }];
    
    NSLog(@"string1 = %@",string1);
    
    
    [self.userNameInput.rac_textSignal subscribeNext:^(id x) {
        NSLog(@"New value = %@",x);
    } error:^(NSError *error) {
        NSLog(@"Error = %@", error);
    } completed: ^{
        NSLog(@"Completed");
    }];
    
    RACSignal *signal = [self.userNameInput.rac_textSignal map:^id (NSString *string) {
        return @(string.length > 5);
    }];
    
    //    RAC(self.button, enabled) = signal;
    
    RAC(self.userNameInput, textColor) = [signal map:^id _Nullable(id  _Nullable value) {
        if ([value boolValue] == YES) {
            return [UIColor greenColor];
        } else {
            return [UIColor redColor];
        }
    }];
    
    self.button.rac_command = [[RACCommand alloc]initWithEnabled:signal signalBlock:^RACSignal *(id  _Nullable input) {
        NSLog(@"Button was pressed");
        return [RACSignal empty];
    }];
}
@end
